/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package coda;

import coda.util.PathUtil;
import coda.util.StringUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 * @author pan
 */
public class Config extends Properties {

    private static final String CONFIG = PathUtil.join(PathUtil.HOME, ".config", "coda.conf");
    private static Config instance = null;

    protected Config() {
        // load mpd settings first, if avail
        String[] conf = new String[] {PathUtil.join(PathUtil.HOME, ".mpdconf"), PathUtil.join("/etc/mpd.conf")};
        for (String c : conf) {
            File f = new File(c);
            if (f.exists()) {
                mergeMPDSettings(f);
                break;
            }
        }
        // load client settings
        System.out.println("reading settings from '" + CONFIG + "'... ");
        try {
            load(new FileInputStream(new File(CONFIG)));
            for (Entry e : this.entrySet()) {
                System.out.println("\t" + e.getKey() + "=" + e.getValue());
            }
            System.out.println("ok");
        } catch (Exception e) {
            System.err.println("nope");
        }
        //TODO: create needed directories (?)
    }
    
    private void mergeMPDSettings(File f) {
        System.out.println("reading settings from '" + f.getAbsolutePath() + "'... ");
        String[] parameters = new String[] {"port", "music_directory"};
        try {
            Scanner scanner = new Scanner(f);
            scanner.useDelimiter(System.getProperty("line.separator"));
            while (scanner.hasNext()) {
                String s = scanner.next();
                if (!s.startsWith("#")) {
                    StringTokenizer t = new StringTokenizer(s);
                    if (t.countTokens() >= 2) {
                        String key = t.nextToken();
                        for (String p : parameters) {
                            if (key.equals(p)) {
                                String value = t.nextToken();
                                setProperty(key, StringUtil.unquote(value));
                                System.out.println("\t" + key + "=" + value);
                                break;
                            }
                        }
                    }
                }
            }
            System.out.println("done");
        } catch (Exception e) {
            System.err.println("failed");
        }
    }

    public static synchronized Config getInstance() {
        if (instance == null) {
            instance = new Config();
        }
        return instance;
    }

    public void save() {
        System.out.print("saving settings...");
        System.out.flush();
        try {
            store(new FileOutputStream(new File(CONFIG)), null);
            System.out.println("done");
        } catch (Exception e) {
            System.out.println("failed");
        }
    }

    public int getInteger(String property, int defaultValue) {
        try {
            return Integer.parseInt(getProperty(property));
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public boolean getBoolean(String property, boolean defaultValue) {
        try {
            return Boolean.parseBoolean(getProperty(property));
        } catch (Exception e) {
            return defaultValue;
        }
    }

}
